

//
//  JCObjectSerializerContext.m
//  JCSDK
//
//  Created by master on 16/2/23.
//  Copyright © 2016年 AdMaster. All rights reserved.
//

#import "JCObjectSerializerContext.h"

@interface JCObjectSerializerContext ()

@property (nonatomic, strong) NSMutableSet *visitedObjects;

@property (nonatomic, strong) NSMutableSet *unvisitedObjects;

@property (nonatomic, strong) NSMutableDictionary *serialiedObjects;

@property (nonatomic, strong) NSMutableSet *saveObjects;

@end

@implementation JCObjectSerializerContext


- (instancetype)initWithRootObject:(id)rootObject {
    if(self = [super init]) {
        _visitedObjects = [NSMutableSet set];
        _unvisitedObjects = [NSMutableSet setWithObject:rootObject];
        _serialiedObjects = [[NSMutableDictionary alloc] init];
        _saveObjects = [NSMutableSet set];
    }
    return self;
}

- (BOOL)hasUnvisitedObjects {
    return [_unvisitedObjects count] > 0;
}

- (void)pushUnvisitedObject:(NSObject *)object {
    NSParameterAssert(object != nil);
    NSLog(@"进去分析队列---%@",object);
    [_saveObjects addObject:object];

    [_unvisitedObjects addObject:object];
}

- (NSObject *)popUnvisitedObject {
    NSObject *object = [_unvisitedObjects anyObject];
    [_unvisitedObjects removeObject:object];
    return object;
}

- (void)addVisitedObject:(NSObject *)object {
    NSParameterAssert(object != nil);
    [_visitedObjects addObject:object];
}

- (BOOL)isVisitedObject:(NSObject *)object {
    return object && [_visitedObjects containsObject:object];
}

- (void)addSerializedObject:(NSDictionary *)serializedObject {
    NSParameterAssert(serializedObject[@"id"] != nil);
    _serialiedObjects[serializedObject[@"id"]] = serializedObject;
}

- (NSArray *)allSerializedObjects {
    return [_serialiedObjects allValues];
}

@end
